package com.eqxiu.iom.msg.config;

import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * @description rabbitTemplate 配置
 * @author wangl
 * @date 2020-05-27
 * @modified by xxx
 */
@Configuration
public class RabbitMQConfig {

    @Bean(name = "userConnectionFactory")
    @Primary
    public ConnectionFactory userConnectionFactory(@Value("${spring.rabbitmq.user.host}") String host
            , @Value("${spring.rabbitmq.user.port}") int port
            , @Value("${spring.rabbitmq.user.username}") String username
            , @Value("${spring.rabbitmq.user.password}") String password
            , @Value("${spring.rabbitmq.user.virtual-host}") String virtual) {
        CachingConnectionFactory userConnectionFactory = new CachingConnectionFactory();
        userConnectionFactory.setHost(host);
        userConnectionFactory.setPort(port);
        userConnectionFactory.setVirtualHost(virtual);
        userConnectionFactory.setUsername(username);
        userConnectionFactory.setPassword(password);
        return userConnectionFactory;
    }

    @Bean(name = "maxcenterConnectionFactory")
    public ConnectionFactory maxcenterConnectionFactory(@Value("${spring.rabbitmq.maxcenter.host}") String host
            , @Value("${spring.rabbitmq.maxcenter.port}") int port
            , @Value("${spring.rabbitmq.maxcenter.username}") String username
            , @Value("${spring.rabbitmq.maxcenter.password}") String password
            , @Value("${spring.rabbitmq.maxcenter.virtual-host}") String virtual) {
        CachingConnectionFactory maxcenterConnectionFactory = new CachingConnectionFactory();
        maxcenterConnectionFactory.setHost(host);
        maxcenterConnectionFactory.setPort(port);
        maxcenterConnectionFactory.setVirtualHost(virtual);
        maxcenterConnectionFactory.setUsername(username);
        maxcenterConnectionFactory.setPassword(password);
        return maxcenterConnectionFactory;
    }

    @Bean(name = "userRabbitTemplate")
    public RabbitTemplate userRabbitTemplate(@Qualifier("userConnectionFactory") ConnectionFactory userConnectionFactory) {
        RabbitTemplate userRabbitTemplate = new RabbitTemplate(userConnectionFactory);
        return userRabbitTemplate;
    }

    @Bean(name = "maxcenterRabbitTemplate")
    public RabbitTemplate maxcenterRabbitTemplate(@Qualifier("maxcenterConnectionFactory") ConnectionFactory maxcenterConnectionFactory) {
        RabbitTemplate maxcenterRabbitTemplate = new RabbitTemplate(maxcenterConnectionFactory);
        return maxcenterRabbitTemplate;
    }

    @Bean(name = "userFactory")
    public SimpleRabbitListenerContainerFactory userFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer
            , @Qualifier("userConnectionFactory") ConnectionFactory userConnectionFactory) {
        SimpleRabbitListenerContainerFactory userFactory = new SimpleRabbitListenerContainerFactory();
        configurer.configure(userFactory, userConnectionFactory);
        return userFactory;
    }

    @Bean(name = "maxcenterFactory")
    public SimpleRabbitListenerContainerFactory maxcenterFactory(
            SimpleRabbitListenerContainerFactoryConfigurer configurer
            , @Qualifier("maxcenterConnectionFactory") ConnectionFactory maxcenterConnectionFactory) {
        SimpleRabbitListenerContainerFactory maxcenterFactory = new SimpleRabbitListenerContainerFactory();
        configurer.configure(maxcenterFactory, maxcenterConnectionFactory);
        return maxcenterFactory;
    }

}
